package com.xbongbong.crm.perform.query.render.edit;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmEditPageRenderService;
import com.xbongbong.paas.domain.entity.PaasProcessDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.CommonHelp;
import com.xbongbong.paas.model.PaasProcessDataModel;
import com.xbongbong.paas.pojo.dto.FormDataUpdateGetDTO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.QuotationErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.SaasSpecialParamPojo;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.pro.permission.pojo.dto.VerifyUpdatePermissionDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.help.FormatProductHelp;
import com.xbongbong.saas.model.QuotationModel;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.help.ProPermissionHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 报价单编辑页面
 * @author 吴峰
 * @date 2022/06/10 16:21
 */
@Service("quotationEditPageRenderServiceImpl")
@Slf4j
public class QuotationEditPageRenderServiceImpl extends AbstractCrmEditPageRenderService {

    @Resource
    private QuotationModel quotationModel;
    @Resource
    private FormatProductHelp formatProductHelp;
    @Resource
    private CommonHelp commonHelp;
    @Resource
    private PaasProcessDataModel paasProcessDataModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.QUOTATION.getCode());
    }

    @Override
    public void validateEditPermission(FormDataUpdateGetDTO formDataUpdateGetDTO) throws XbbException {
        //新版本,复制跳过编辑权限校验
        Boolean quotationBoolean = false;
        if ( !Objects.equals(formDataUpdateGetDTO.getNewVersionAttr(), null) ) {
            quotationBoolean = formDataUpdateGetDTO.getNewVersionAttr();
        }
        if ( quotationBoolean || (formDataUpdateGetDTO.getSaasSpecialParamPojo() != null && Objects.equals(formDataUpdateGetDTO.getSaasSpecialParamPojo().getFromCopy(), BasicConstant.ONE))) {
            return;
        }
        VerifyUpdatePermissionDTO verifyUpdatePermissionDTO = new VerifyUpdatePermissionDTO();
        BeanUtil.copyProperties(formDataUpdateGetDTO, verifyUpdatePermissionDTO);
        ProPermissionHelp.verifyEditPermission(verifyUpdatePermissionDTO);
    }
    @Override
    public PaasFormDataEntityExt getData(FormDataUpdateGetDTO formDataUpdateGetDTO) throws XbbException{
        PaasFormDataEntityExt paasFormDataEntityExt = super.getData(formDataUpdateGetDTO);
        String corpid = formDataUpdateGetDTO.getCorpid();
        Integer businessType = formDataUpdateGetDTO.getBusinessType();
        SaasSpecialParamPojo saasSpecialParamPojo = formDataUpdateGetDTO.getSaasSpecialParamPojo();
        boolean newVersionAndUpdate = Objects.equals(businessType, XbbRefTypeEnum.QUOTATION.getCode()) && !(Objects.nonNull(saasSpecialParamPojo) && Objects.equals(saasSpecialParamPojo.getFromCopy(), BasicConstant.ONE) && !Objects.equals(saasSpecialParamPojo.getNewVersion(), BasicConstant.ONE));
        boolean isOpenWorkflow = commonHelp.isOpenWorkFlow(corpid);
        if ( newVersionAndUpdate && !isOpenWorkflow) {
            // 报价单编辑/新版本需要判断是否有新版本在审批中，复制不需要
            String groupNumber = FastJsonHelper.getStringOrDefaultFromFormData(paasFormDataEntityExt.getData(), QuotationEnum.GROUP_NUMBER.getAttr(), "");
            List<PaasProcessDataEntity> entities = paasProcessDataModel.getInApprovalData(XbbRefTypeEnum.QUOTATION.getCode(), QuotationEnum.GROUP_NUMBER.getAttr(), groupNumber, corpid);

            if ( entities != null && entities.size() > 0L ) {
                throw new XbbException(QuotationErrorCodeEnum.API_ERROR_246009);
            }
        }
        return paasFormDataEntityExt;
    }
    @Override
    public ParseSingleRowDataVO formatData(FormDataUpdateGetDTO formDataUpdateGetDTO, HandlerExplainVO handlerExplainVO, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO(formDataUpdateGetDTO.getCorpid(), formDataUpdateGetDTO.getDataId(), formDataUpdateGetDTO.getBusinessType(), false, null);
        // saas特殊格式转换
        JSONObject data = paasFormDataEntityExt.getData();
        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMapByList(handlerExplainVO.getHeadList());
        saasAnalysisDataDTO.setExplainMap(explainMap);
        quotationModel.formatData(saasAnalysisDataDTO, data);
        boolean formCopy = Objects.nonNull(formDataUpdateGetDTO.getSaasSpecialParamPojo()) && Objects.equals(formDataUpdateGetDTO.getSaasSpecialParamPojo().getFromCopy(), BasicConstant.ONE);
        formatProductHelp.formatProduct(saasAnalysisDataDTO,data,handlerExplainVO.getHeadList(), formCopy);

        data.put(FieldTypeEnum.COUSERID.getAlias(), handlerExplainVO.getCoUserId());
        data.put(FieldTypeEnum.SERIALNO.getAlias(), paasFormDataEntityExt.getSerialNo());
        data.put(FieldTypeEnum.OWNERID.getAlias(), handlerExplainVO.getOwnerId());
        data.put(FieldTypeEnum.DEPARTMENTID.getAlias(), paasFormDataEntityExt.getDepartmentId());
        ParseSingleRowDataVO parseSingleRowDataVO = parseSingleRowData(formDataUpdateGetDTO, data, explainMap, handlerExplainVO.getUserMap(), handlerExplainVO.getDepartmentMap());
        // 模板数据
        paasFormDataEntityExt.setData(parseSingleRowDataVO.getResult());
        return parseSingleRowDataVO;
    }

    @Override
    public void businessHandleExplain(FormDataUpdateGetDTO formDataUpdateGetDTO, List<FieldAttrEntity> list, PaasFormEntityExt form, PaasFormDataEntityExt dataEntity) throws XbbException {
        String corpid = formDataUpdateGetDTO.getCorpid();
        SaasSpecialParamPojo saasSpecialParamPojo = formDataUpdateGetDTO.getSaasSpecialParamPojo();
        boolean formCopy = Objects.nonNull(saasSpecialParamPojo) && Objects.equals(saasSpecialParamPojo.getFromCopy(), BasicConstant.ONE);
        // 编辑时saas字段显示特殊逻辑
        FormatExplainDTO formatExplainDTO = new FormatExplainDTO(corpid, list, formCopy, saasSpecialParamPojo, dataEntity, formDataUpdateGetDTO.getLoginUser());

        quotationModel.formatUpdateExplain(formatExplainDTO);
    }

    @Override
    public void handelSaasSpecialUpdate(List<FieldAttrEntity> explainList, Integer businessType, PaasFormDataEntityExt formDataEntityExt, boolean formCopy, Integer distributorMark) throws XbbException {

    }
}
